package com.example.zengpengcheng0526;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public  class MyService extends Service {

        public MyService() {
        }

        @Override
        public void onCreate() {
            super.onCreate();
        }
        @Override
        public IBinder onBind(Intent intent) {
            Log.d( "onBind: ","-----------------");
            MyBinder myBinder=new MyBinder();
            return myBinder;
        }
        public class MyBinder extends Binder {
            public void bcheck(String a,String b){
                check(a,b);
            }
        }

        public boolean check(String uname,String upw){
            if (TextUtils.isEmpty(uname)||TextUtils.isEmpty(upw)){
                Toast.makeText(getApplicationContext() , "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                return false;
            }else{
                if (uname.length()<6||upw.length()<6){
                    Toast.makeText(getApplicationContext(), "用户名或密码最少6位", Toast.LENGTH_SHORT).show();
                    return false;
                }else {
                    return true;
                }
            }
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            String uname=intent.getStringExtra("uname");
            String upw=intent.getStringExtra("upw");
            User user= (User) intent.getSerializableExtra("user");
            boolean check = check(uname, upw);
            final Intent intent1=new Intent();
          /*  new Thread(new Runnable() {
                @Override
                public void run() {
                                intent1.setAction("com.example.zengpengcheng0526.LoginActivity");
                                //课堂作业部分
                    for (int i = 0; i <20 ; i++) {
              intent1.putExtra("ss", i+"");
              sendBroadcast(intent1);
                        SystemClock.sleep(100);
                    }
                }
            }).start();*/
//
            if (check) {
                if (uname.equals(user.getUsername())) {
                    if (uname.equals(user.getUsername()) && upw.equals(user.getPw())) {
                        intent1.putExtra("flag", true);
                        intent1.putExtra("msg", "登录成功");
                        intent1.putExtra("gbuser",user);
                        sendBroadcast(intent1);
                    } else {
                        intent1.putExtra("flag", false);
                        intent1.putExtra("msg", "用户名或密码错误");
                        sendBroadcast(intent1);
                    }
                } else {
                    intent1.putExtra("flag", false);
                    intent1.putExtra("msg", "该用户未注册");
                    sendBroadcast(intent1);
                }
            }
            return super.onStartCommand(intent, flags, startId);
        }


    @Override
        public boolean onUnbind(Intent intent) {
            return super.onUnbind(intent);
        }
    }
